package com.api.controller;

import com.api.config.Result;
import com.api.pojo.User;
import com.api.service.UserService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: UserController
 * @Description: 用户操作接口
 * @author: QT
 * @date: 2020年10月12日 上午16:21:32
 */
@Api(tags = "UserController", description = "用户操作接口")
@RestController
@RequestMapping("/user")
public class UserController {
    public static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * @param pageNow 当前页
     * @param pageSize 每页大小
     * @return
     */
    @ApiOperation("获取员工列表")
    @GetMapping("/queryUserList")
    public @ResponseBody
    Result queryUserListResult(@ApiParam(name = "pageNow", value = "页码", required = true, defaultValue = "1")
                               @RequestParam int pageNow,
                               @ApiParam(name = "pageSize", value = "每页数量", required = true, defaultValue = "3")
                               @RequestParam int pageSize) {
        List<User> userList = userService.queryUserList(pageNow,pageSize);
        log.info("获取员工列表："+userList);
        return Result.succResult(userList);
    }

    @ApiOperation("按员工ID获取员工列表")
    @GetMapping("/queryUserById/{id}")
    public @ResponseBody
    Result queryUserResultById(@ApiParam(name = "id", value = "用户ID", required = true)
                               @PathVariable int id) {
        User user = userService.queryUserById(id);
        return Result.succResult(user);
    }

    @ApiOperation("员工新增")
    @PostMapping("/createUser")
    public @ResponseBody
    Result createUserResult(@ApiParam(value = "用户信息", required = true) @RequestBody User user) throws DataAccessException {
        try{
            int count = userService.createUser(user);
            if (count == 1) {
                return Result.succResult("CreateUser Success");
            }else{
                return Result.failResult("CreateUser Fail");
            }
        }catch (DataAccessException e){
            log.info("Exception: "+e);
            return Result.failResult("CreateUser Fail："+e);
        }
    }

    @ApiOperation("员工修改")
    @PostMapping("/updateUser/{id}")
    public @ResponseBody
    Result updateUserResult(
            @ApiParam(value = "用户ID", required = true) @PathVariable Integer id,
            @ApiParam(value = "Token", required = true) @RequestHeader String token,
            @ApiParam(value = "用户信息", required = true)  @RequestBody User user) throws DataAccessException {
        try {
            if ("Token20210531".equals(token)) {
                int count = userService.updateUser(id, user);
                if (count == 1) {
                    return Result.succResult("UpdateUser Success");
                }else{
                    return Result.succResult("UpdateUser Fail");
                }
            } else {
                return Result.failResult("Token Validate Fail");
            }
        } catch (DataAccessException e) {
            log.info("Exception: "+e);
            return Result.failResult("UpdateUser Fail："+e);
        }
    }

    @ApiOperation("员工删除")
    @DeleteMapping("/deleteUser/{id}")
    public @ResponseBody
    Result deleteUserResult(@ApiParam(value = "用户ID", required = true) @PathVariable Integer id) throws DataAccessException{
        try{
            int count = userService.deleteUser(id);
            if (count == 1) {
                return Result.succResult("DeleteUser Success");
            }else{
                return Result.succResult("DeleteUser Fail");
            }
        }catch (DataAccessException e){
            log.info("Exception: "+e);
            return Result.failResult("DeleteUser Fail："+e);
        }
    }

    @ApiOperation("员工登录")
    @GetMapping("/login")
    public @ResponseBody
    Result loginResult(
            @ApiParam(value = "用户名", required = true) @RequestParam String username,
            @ApiParam(value = "密码", required = true) @RequestParam String password) throws DataAccessException{
        try{
            int count = userService.login(username,password);
            if (count > 0) {
                return Result.succResult("Login Success");
            }else{
                return Result.succResult("Login Fail");
            }
        }catch (DataAccessException e){
            log.info("Exception: "+e);
            return Result.failResult("Login Fail："+e);
        }
    }
}
